home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Screenblankers / GBlanker / GSource / Blankers / Plasma / blank.c next >
C/C++ Source or Header  |  1996-09-26  |  5KB  |  197 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include "/includes.h"
  11.  
  12. #define DIMEN 0
  13. #define EXTEN 2
  14. #define DIVIS 4
  15. #define MODE  6
  16.  
  17. #define MAXITER 16
  18.  
  19. LONG Range[MAXITER], Hei, Wid, Minor, Offx, Offy, ExtendPal, Divisions;
  20. Triplet *ColorTable = 0L;
  21. BYTE Cl, Dimen;
  22.  
  23. #include "Plasma_rev.h"
  24. STATIC const UBYTE VersTag[] = VERSTAG;
  25.  
  26. #define Val( x, y ) ( ReadPixel( &Scr->RastPort, (x)+Offx, (y)+Offy))
  27. #define max( x, y ) ( (x) > (y) ? (x) : (y) )
  28.  
  29. VOID Defaults( PrefObject *Prefs )
  30. {
  31.     Prefs[DIMEN].po_Active = 0;
  32.     Prefs[EXTEN].po_Level = 2;
  33.     Prefs[DIVIS].po_Active = 2;
  34.     Prefs[MODE].po_ModeID = getTopScreenMode();
  35.     Prefs[MODE].po_Depth = 4;
  36. }
  37.  
  38. void SetPixel( struct RastPort *R, LONG i, LONG x, LONG y, LONG v )
  39. {
  40.     if(( x < Wid )&&( y < Hei ))
  41.     {
  42.         SetAPen( R, ( v + Cl + RangeRand( Range[i] ) - Range[i]/2 ) % Cl + 1 );
  43.         WritePixel( R, x + Offx, y + Offy );
  44.     }
  45. }
  46.  
  47. LONG GroovyRound( LONG Val )
  48. {
  49.     LONG i;
  50.  
  51.     for( i = -1; Val; Val >>= 1, i++ );
  52.  
  53.     return 1 << i;
  54. }
  55.  
  56. LONG PlasmaRect( struct Screen *Scr, LONG i, LONG Size, LONG Step, LONG sx,
  57.                 LONG sy )
  58. {
  59.     LONG RetVal = OK, z = 0, x, y, nx, ny, hx, hy, MaxX, MaxY;
  60.     struct RastPort *R = &Scr->RastPort;
  61.     
  62.     MaxX = min( sx + Size, Minor );
  63.     MaxY = min( sy + Size, Minor );
  64.     
  65.     for( y = sy; ( y < MaxY )&&( RetVal == OK ); y += Step )
  66.     {
  67.         ny = ( y + Step ) % Minor;
  68.         hy = ( y + Step/2 ) % Minor;
  69.         for( x = sx; ( x < MaxX )&&( RetVal == OK ); x += Step )
  70.         {
  71.             nx = ( x + Step ) % Minor;
  72.             hx = ( x + Step/2 ) % Minor;
  73.             if(( x + Step/2 ) < Minor )
  74.                 SetPixel( R, i, hx, y, ( Val( x, y ) + Val( nx, y )) / 2 );
  75.             if(( y + Step/2 ) < Minor )
  76.                 SetPixel( R, i, x, hy, ( Val( x, y ) + Val( x, ny )) / 2 );
  77.             if(( x + Step/2 ) < Minor &&( y + Step/2 ) < Minor )
  78.                 SetPixel( R, i, hx, hy, ( Val( x, y ) + Val( nx, y ) +
  79.                                          Val( x, ny ) + Val( nx, ny ))/4 );
  80.             if(!( ++z % 300 ))
  81.                 RetVal = ContinueBlanking();
  82.         }
  83.     }
  84.     
  85.     return ( RetVal == OK )? ContinueBlanking() : RetVal;
  86. }
  87.  
  88. LONG DrawPlasma( struct Screen *Scr )
  89. {
  90.     LONG RetVal = OK, x, y, i, Step, Side, MaxCells = 0, yCells;
  91.     
  92.     Side = GroovyRound( Minor ) >> Divisions;
  93.     for( yCells = 0, y = 0; ( y < Minor )&&( RetVal == OK ); y += Side )
  94.     {
  95.         yCells++;
  96.         for( x = 0; x < Minor; x += Side )
  97.         {
  98.             SetPixel( &Scr->RastPort, 0, x, y, 0 );
  99.             MaxCells++;
  100.         }
  101.     }
  102.  
  103.     if( Divisions )
  104.     {
  105.         for( i = 1, Step = Side; ( RetVal == OK )&&( Step > 1 );
  106.             Step >>= 1, i++ )
  107.         {
  108.             LONG *Drawn, Cells = MaxCells;
  109.         
  110.             if( Drawn = AllocVec( sizeof( LONG ) * Cells, MEMF_CLEAR ))
  111.             {
  112.                 do
  113.                 {
  114.                     LONG j = -1, Pick = RangeRand( Cells ) + 1;
  115.                     
  116.                     while( Pick ) !Drawn[++j] && --Pick;
  117.                     Drawn[j] = TRUE;
  118.                     RetVal = PlasmaRect( Scr, i, Side, Step,
  119.                                         ( j / yCells ) * Side,
  120.                                         ( j % yCells ) * Side );
  121.                 }    
  122.                 while(( RetVal == OK )&& --Cells );
  123.                 FreeVec( Drawn );
  124.             }
  125.         }
  126.     }
  127.     else
  128.         for( i = 0, Step = Side; ( RetVal == OK )&&( Step > 1 ); Step >>= 1 )
  129.             RetVal = PlasmaRect( Scr, ++i, Minor, Step, 0, 0 );
  130.     
  131.     return RetVal;
  132. }
  133.  
  134. LONG Blank( PrefObject *Prefs )
  135. {
  136.     struct Screen *Scr;
  137.     struct Window *Wnd;
  138.     LONG i, RetVal = OK;
  139.     
  140.     Scr = OpenScreenTags( NULL, SA_Depth, Prefs[MODE].po_Depth, SA_Quiet, TRUE,
  141.                          SA_DisplayID, Prefs[MODE].po_ModeID, SA_Behind, TRUE,
  142.                          SA_Overscan, OSCAN_STANDARD, TAG_DONE );
  143.     if( Scr )
  144.     {
  145.         Cl = 1L << Prefs[MODE].po_Depth - 1;
  146.         Wid = Scr->Width;
  147.         Hei = Scr->Height;
  148.         if( Prefs[DIMEN].po_Active )
  149.         {
  150.             Minor = Hei;
  151.             if( Wid > Hei )
  152.                 Offx = ( Wid - Hei )/ 2;
  153.             else
  154.                 Offx = 0L;
  155.             Offy = 0L;
  156.         }
  157.         else
  158.         {
  159.             Minor = Wid;
  160.             Offx = 0L;
  161.             if( Hei > Wid )
  162.                 Offy = ( Hei - Wid )/ 2;
  163.             else
  164.                 Offy = 0;
  165.         }
  166.         for( i = 0; i < MAXITER; i++ )
  167.             Range[i] = 1L << max( Prefs[MODE].po_Depth - i, 1 );
  168.         ExtendPal = Prefs[EXTEN].po_Level;
  169.         Divisions = Prefs[DIVIS].po_Active;
  170.         ColorTable = RainbowPalette( Scr, 0L, 1, ExtendPal );
  171.         Wnd = BlankMousePointer( Scr );
  172.         
  173.         while( RetVal == OK )
  174.         {
  175.             ScreenToFront( Scr );
  176.             SetRast( &Scr->RastPort, 0L );
  177.             RetVal = DrawPlasma( Scr );
  178.             for( i = 0; ( i < Wid * Hei / 100 )&&( RetVal == OK ); i++ )
  179.             {
  180.                 WaitTOF();
  181.                 if(!( i % 8 ))
  182.                 {
  183.                     RainbowPalette( Scr, ColorTable, 1, ExtendPal );
  184.                     RetVal = ContinueBlanking();
  185.                 }
  186.             }
  187.         }
  188.         RainbowPalette( NULL, ColorTable, 1, ExtendPal );
  189.         UnblankMousePointer( Wnd );
  190.         CloseScreen( Scr );
  191.     }
  192.     else
  193.         RetVal = FAILED;
  194.     
  195.     return RetVal;
  196. }
  197.